# -*- coding: utf-8 -*-
# Author       : Xinwu
# Email        : lexinwu@genenergy.cn
# Describe     :
# Created Time : 2023-06-28 18:26:37
# Last Modified: 2023-06-28 19:46:40
# 数据下载自: https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE171524
# paper: A molecular single-cell lung atlas of lethal COVID-19 再将 meta data 附在 adata.obs 中

import scanpy as sc
import pandas as pd

def pp(file):
    adata = sc.read_csv(file).T
    return adata
file_list = ["GSM5226574_C51ctr_raw_counts.csv","GSM5226575_C52ctr_raw_counts.csv","GSM5226576_C53ctr_raw_counts.csv",
             "GSM5226577_C54ctr_raw_counts.csv","GSM5226578_C55ctr_raw_counts.csv","GSM5226579_C56ctr_raw_counts.csv",
             "GSM5226580_C57ctr_raw_counts.csv","GSM5226581_L01cov_raw_counts.csv","GSM5226582_L03cov_raw_counts.csv",
             "GSM5226583_L04cov_raw_counts.csv","GSM5226584_L04covaddon_raw_counts.csv","GSM5226585_L05cov_raw_counts.csv",
             "GSM5226586_L06cov_raw_counts.csv","GSM5226587_L07cov_raw_counts.csv","GSM5226588_L08cov_raw_counts.csv",
             "GSM5226589_L09cov_raw_counts.csv","GSM5226590_L10cov_raw_counts.csv","GSM5226591_L11cov_raw_counts.csv",
             "GSM5226592_L12cov_raw_counts.csv","GSM5226593_L13cov_raw_counts.csv","GSM5226594_L15cov_raw_counts.csv",
             "GSM5226595_L16cov_raw_counts.csv","GSM5226596_L17cov_raw_counts.csv","GSM5226597_L18cov_raw_counts.csv",
             "GSM5226598_L19cov_raw_counts.csv","GSM5226599_L21cov_raw_counts.csv","GSM5226600_L22cov_raw_counts.csv"]
out = []
for file in file_list:
    out.append(pp(f'/public/home/lxw/08.Personal/test_scanpy/mtx/{file}'))
adata = sc.concat(out)
meta_data = pd.read_table('/public/home/lxw/08.Personal/test_scanpy/metadata.txt',header=0)
adata = adata[adata.obs.index.isin(meta_data.Barcode.values)] # 把 metadata 中没有的 barcode 去除掉, 使得下面的属性附加能正常进行。
adata.obs['Sample'] = meta_data.Sample.values
adata.obs['CellType'] = meta_data.CellType.values
adata.obs['group'] = meta_data.group.values
adata.write('project_COVID.h5ad', compression = 'gzip')
